package com.jsy.goods.controller;

import com.github.pagehelper.PageInfo;
import com.jsy.common.constant.StatusCode;
import com.jsy.common.vo.Result;
import com.jsy.goods.pojo.Brand;

import com.jsy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:www.jsy.com
 * @Description:
 * @Date www.jsy.com
 *****/

@RestController
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;

    /***
     * Brand分页条件搜索实现
     * @param brand: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Brand>> findPage(@RequestBody(required = false) Brand brand, @PathVariable  int page, @PathVariable  int size){
        //调用BrandService实现分页条件查询Brand
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result<PageInfo<Brand>>("查询成功！", pageInfo);
    }

    /***
     * Brand分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Brand>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用BrandService实现分页查询Brand
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result<PageInfo<Brand>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param brand 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Brand>> findList(@RequestBody(required = false) Brand brand){
        //调用BrandService实现条件查询Brand
        List<Brand> list = brandService.findList(brand);
        return new Result<List<Brand>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用BrandService实现根据主键删除
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Brand数据
     * @param brand 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody Brand brand){
        //调用BrandService实现修改Brand
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Brand数据
     * @param brand 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        //调用BrandService实现添加Brand
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询Brand数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Integer id){
        //调用BrandService实现根据主键查询Brand
        Brand brand = brandService.findById(id);
        return new Result<Brand>("查询成功！", brand);
    }

    /***
     * 查询Brand全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Brand>> findAll(){
        //调用BrandService实现查询所有Brand
        //测试代码
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        List<Brand> list = brandService.findAll();
        return new Result<List<Brand>>("查询成功！", list);
    }
    /***
     * 根据分类id查询Brand数据
     * @return 查询结果
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<Brand>> findByCategoryId(@PathVariable Integer categoryId){
        //调用BrandService实现查询所有Brand
        List<Brand> list = brandService.findByCategoryId(categoryId);
        return new Result<List<Brand>>("查询成功！", list);
    }


}
